﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="view.aspx.cs" Inherits="skvote.vote" %>

<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <%
        if (string.IsNullOrEmpty(Request["sid"]))
        {
            Response.Write("<script language='javascript' type='text/javascript'>alert('主题参数sid不能为空!');window.close()</script>");
            Response.End();
        }
        try
        {
            Convert.ToInt32(Request["sid"]);
        }
        catch (Exception)
        {
            Response.Write("<script language='javascript' type='text/javascript'>alert('主题参数sid只能是数字');window.close()</script>");
            Response.End();
        }
        skvote.config cfg = new skvote.config();
        if (!cfg.getAllowView() && (Session["admin"]==null || Session["admin"]=="") && !string.IsNullOrEmpty(Request["submit"]) && Request["submit"].Trim()=="查看")
        {
            Response.Write("<script language='javascript'>alert('只有管理员可以查看!');window.close();</script>");
            Response.End();
        }
        if (!string.IsNullOrEmpty(Request["submit"]) && Request["submit"].Trim() == "投票")
        {
            if (cfg.getRestrictIP())
            {
                string clientIP = Request.ServerVariables["remote_addr"];
                OleDbConnection tempconn = new skvote.conn().CreateConnection();
                bool voted = false;
                try
                {
                    OleDbCommand tempcmd = new OleDbCommand("select * from xh_ip where ip=\"" + clientIP + "\" and sid=" + Request["sid"] + " and datediff('h',vtime,now())<" + cfg.getRefuseTime().ToString(), tempconn);
                    OleDbDataReader tempdr = tempcmd.ExecuteReader();
                    if (tempdr.HasRows)
                    {
                        voted = true;
                    }
                    tempdr.Close();
                    if (!voted)
                    {
                        tempcmd.CommandText = "insert into xh_ip(sid,ip,vtime) values(" + Request["sid"] + ",\"" + clientIP + "\",now())";
                        tempcmd.ExecuteNonQuery();
                        tempcmd.CommandText = "delete from xh_ip where sid=" + Request["sid"].Trim() + " and ip=\"" + clientIP +"\" and datediff('h',vtime,now())>="+cfg.getRefuseTime().ToString();
                        tempcmd.ExecuteNonQuery();
                    }
                    tempcmd.Dispose();
                }
                finally
                {
                    tempconn.Close();                       
                }
                if (voted)
                {
                    Response.Write("<script language='javascript' type='text/javascript'>alert('您已经投过票了!');window.close();</script>");
                    Response.End();
                }
            }
            string submitparams = "";
            OleDbConnection tconn = new skvote.conn().CreateConnection();
            try
            {
                OleDbCommand tcmd = new OleDbCommand("select * from xh_title", tconn);
                OleDbDataReader tdr = tcmd.ExecuteReader();
                while (tdr.Read())
                {
                    string ids = string.IsNullOrEmpty(Request[tdr["id"].ToString()]) ? "" : Request[tdr["id"].ToString()];
                    submitparams += ids + ",";
                    string[] idarr = ids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string id in idarr)
                    {
                        OleDbConnection qconn = new skvote.conn().CreateConnection();
                        try
                        {                            
                            OleDbCommand qcmd = new OleDbCommand("update xh_question set ps=ps+1 where id=" + id.Trim(), qconn);
                            qcmd.ExecuteNonQuery();
                            qcmd.CommandText = "update xh_title set vcount=vcount+1 where id="+tdr["id"].ToString();
                            qcmd.ExecuteNonQuery();
                        }
                        finally
                        {
                            qconn.Close();
                        }                        
                    }
                }
                tdr.Close();
                tcmd.Dispose();
            }
            finally
            {
                tconn.Close();
            }
            if (string.IsNullOrEmpty(submitparams))
            {
                Response.Write("<script language='javascript' type='text/javascript'>alert('您没有选择投票项!');window.close()</script>");
                Response.End();
            }
            else
            {
                submitparams = submitparams.Substring(0, submitparams.Length - 1);
            }            
            if (cfg.getUserInfo())
            {
                OleDbConnection tempconn = new skvote.conn().CreateConnection();
                bool binsert = false;
                try
                {
                    string name = skvote.function.nohtml(Request["name"]);
                    string sex = string.IsNullOrEmpty(Request["sex"]) ? "" : skvote.function.nohtml(Request["sex"]);
                    string idcard = skvote.function.nohtml(Request["idcard"]);
                    string tel = string.IsNullOrEmpty(Request["telephone"]) ? "" : skvote.function.nohtml(Request["telephone"]);
                    string address = string.IsNullOrEmpty(Request["address"]) ? "" : skvote.function.nohtml(Request["address"]);
                    string email = string.IsNullOrEmpty(Request["email"]) ? "" : skvote.function.nohtml(Request["email"]);
                    string content = string.IsNullOrEmpty(Request["remarks"]) ? "" : skvote.function.nohtml(Request["remarks"]);
                    string sid = skvote.function.nohtml(Request["sid"]);
                    string qids = skvote.function.nohtml(submitparams);
                    string ip = skvote.function.nohtml(Request.ServerVariables["remote_addr"]);
                    OleDbCommand tempcmd = new OleDbCommand("select * from xh_userinfo where name=\"" + name + "\" and idcard=\"" + idcard + "\" and email=\"" + email+"\"", tempconn);
                    OleDbDataReader tempdr = tempcmd.ExecuteReader();
                    if (!tempdr.HasRows)
                    {
                        tempdr.Close();
                        tempcmd.CommandText = "insert into xh_userinfo([name],[sex],[idcard],[tel],[address],[email],[content],[sid],[qids],[ip]) values(\"" + name + "\",\"" + sex + "\",\"" + idcard + "\",\"" + tel + "\",\"" + address + "\",\"" + email + "\",\"" + content + "\"," + sid + ",\"" + qids + "\",\"" + ip + "\")";
                        tempcmd.ExecuteNonQuery();
                    }
                    tempdr.Close();
                    tempdr.Dispose();
                }
                finally
                {
                    tempconn.Close();
                }
            }
        }
    %>
<html>

<head>
<%skvote.function.header(Response);%>
<title>单考投票系统--查看投票结果</title>
<style type="text/css">
.v_XHList{
background-color: #E1F0FF}
.v_XHList2{
background-color: #8EC7FF}
</style>
</head>

<body>

<div align="center">
	<%OleDbConnection conn = new skvote.conn().CreateConnection();
   OleDbCommand cmd = new OleDbCommand("select * from xh_title where sid=" + Request["sid"].Trim(), conn);
   OleDbDataReader dr = cmd.ExecuteReader();
   while (dr.Read())
   {         
    %>
	<table width="40%" id="table1" cellspacing="1" cellpadding="4" bgcolor="#E1F0FF">
		<tr>
			<th colspan="4" bgcolor="#8EC7FF"><%=dr["title"].ToString()%></th>
		</tr>
		<tr>
			<td colspan="4" align="center" bgcolor="#BFDFFF">该问题总投票人数:<%=dr["vcount"].ToString()%></td>
		</tr>
	    <%
            OleDbConnection qconn = new skvote.conn().CreateConnection();
            OleDbCommand qcmd = new OleDbCommand("select * from xh_question where tid=" + dr["id"].ToString(), qconn);
            OleDbDataReader qdr = qcmd.ExecuteReader();
            int i = 0;
            while (qdr.Read())
            {
                i++;
                int width = Convert.ToInt32(dr["vcount"]) != 0 ? (int)Math.Ceiling(80 * Convert.ToDouble(qdr["ps"]) / Convert.ToDouble(dr["vcount"])) : 0;
        %>
		<tr onMouseOver="this.className='v_XHList2';" onMouseOut="this.className='v_XHList';">
			<td><%=qdr["question"].ToString()%></td>
			<td width="10%" nowrap><b><%=qdr["ps"].ToString() %></b>票</td>
			<td width="80" align="left">
			<table border="0" width="<%=width%>" cellspacing="0" cellpadding="0" height="5" bgcolor="<%=ShowColor(i)%>">
				<tr>
					<td></td>
				</tr>
			</table>
			</td>
			<td width="18%" nowrap>占<b><%if (Convert.ToInt32(dr["vcount"]) != 0) Response.Write((Convert.ToDouble(qdr["ps"]) / Convert.ToDouble(dr["vcount"])).ToString("##.##%"));%></b></td>
		</tr>
	<%}
            qdr.Close();
            qcmd.Dispose();
            qconn.Close();
        %>
	</table><br>
	<%}	    
   dr.Close();
   cmd.Dispose();   
   conn.Close();   
	%>
</div>
<div align="center">
	<input class="btn" type="button" name="button" value="关闭" onClick="javascript:window.close()">
</div>

</body>

</html>
